# Maintainer: Yang Zheng <yz757865041@gmail.com>
# Contributors: Marcin Sielski <marcin.sielski(at)gmail.com>

_realname=paho.mqtt.c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.14
pkgrel=1
pkgdesc="Eclipse Paho MQTT C client (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'aur: paho-mqtt-c'
)
msys2_repository_url='https://github.com/eclipse-paho/paho.mqtt.c'
url='https://eclipse-paho.github.io/paho.mqtt.c/MQTTClient/html/'
license=('spdx:EPL-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "unzip")
depends=("${MINGW_PACKAGE_PREFIX}-openssl")
source=("https://github.com/eclipse-paho/paho.mqtt.c/archive/v${pkgver}/${_realname}-${pkgver}.zip")
sha256sums=('ad67f3920b4dc618867c573626f6dbddc213d3f759abbdb9d785f7f85d086e41')
noextract=("${_realname}-${pkgver}.zip")

prepare() {
  unzip -q "${_realname}-${pkgver}.zip"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -D_WINDOWS"
  CXXFLAGS+=" -D_WINDOWS"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DPAHO_BUILD_STATIC=ON \
      -DPAHO_WITH_SSL=ON \
      -DPAHO_BUILD_DOCUMENTATION=OFF \
      -DPAHO_BUILD_SAMPLES=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}


package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
